package com.tangxz.sso.server.controller;

import com.tangxz.sso.core.conf.Conf;
import com.tangxz.sso.core.entity.User;
import com.tangxz.sso.core.utils.CookieUtil;
import com.tangxz.sso.core.utils.JedisUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import redis.clients.jedis.Jedis;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: 唐小尊
 * @email: 1171702529@qq.com
 * @cate: 2021/07/11 11:00
 */
@Controller
public class UserController {
    @RequestMapping("/")
    public String index(Model model, HttpServletRequest request) {
        User user = (User) request.getAttribute(Conf.SSO_USER);
        model.addAttribute(Conf.SSO_USER, user);
        return "index";
    }

    @RequestMapping(Conf.SSO_LOGIN)
    public String login(Model model, HttpServletRequest request, HttpServletResponse response) {
        String redirectURL = request.getParameter(Conf.REDIRECT_URL);
        model.addAttribute(Conf.REDIRECT_URL, redirectURL);
        model.addAttribute("errorMsg", request.getParameter("errorMsg"));
        return "login";
    }

    @PostMapping("/doLogin")
    public String doLogin(HttpServletRequest request,
                          HttpServletResponse response,
                          RedirectAttributes redirectAttributes,
                          String username,
                          String password,
                          String redirect_url) {
        if (!username.equals("user") || !password.equals("123456")) {
            String parameter = request.getParameter(Conf.REDIRECT_URL);
            if (parameter != null && !parameter.isEmpty()) {
                redirectAttributes.addAttribute(Conf.REDIRECT_URL, parameter);
            }
            redirectAttributes.addAttribute("errorMsg", "密码或用户名错误");
            return "redirect:/login";
        }

        //存用户
        User user = new User(1, "user", "123456");
        //存cookie
        CookieUtil.set(response, Conf.SSO_USER, String.valueOf(user.getId()), true);
        //存redis
        JedisUtil.setObjectValue(String.valueOf(user.getId()), user, 60 * 60 * 24 * 7);

        return redirect_url == null || redirect_url.isEmpty() ? "/" : ("redirect:" + redirect_url);
    }

    /**
     * Logout
     *
     * @param request
     * @param redirectAttributes
     * @return
     */
    @RequestMapping(Conf.SSO_LOGOUT)
    public String logout(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(Conf.SSO_USER)) {
                    JedisUtil.del(cookie.getValue());
                    cookie.setMaxAge(0);//设置过期时间为0秒后
                    break;
                }
            }
        }
        //带参重定向
        redirectAttributes.addAttribute(Conf.REDIRECT_URL, request.getParameter(Conf.REDIRECT_URL));
        return "redirect:/";
    }
}
